package com.ncmmall.mall.controller.business.live;

import com.ncmmall.application.business.live.LiveChannelApplication;
import com.feijin.commons.webs.Webs;
import com.ncmmall.domain.business.live.LiveChannel;
import com.ncmmall.domain.business.live.QLiveChannel;
import com.ncmmall.mall.params.PageParams;
import com.ncmmall.querychannel.QueryPage;
import com.ncmmall.querychannel.QueryPageRequest;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 直播分类管理
 *
 * @author Samuel
 * @date 2020-9-2 14:56:21
 */

@Controller
@RequestMapping(value = "/live/channel")
@Slf4j
public class LiveChnnelController {

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private LiveChannelApplication liveChannelApplication;

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model, PageParams pageParams, LiveChannel params) {

        QueryPageRequest pageRequest = new QueryPageRequest().setPageNo(pageParams.getPageNo()).setPageSize(pageParams.getPageSize());

        Map<String, Object> map = Maps.newHashMap();
        String sql = buildSql("select g from LiveChannel g where 1 = 1 ", map, params);

        QueryPage<LiveChannel> page = queryChannelService.findPage(sql, pageRequest.setParams(map), LiveChannel.class);

        model.addAttribute("page", page);
        model.addAttribute("params", params);

        return "mall/live/channel/list";
    }

    /**
     * 条件查询
     */
    private String buildSql(String sql, Map<String, Object> map, LiveChannel params) {

        if (!Strings.isNullOrEmpty(params.getName())) {
            sql += " and g.name like :name ";
            map.put("name", "%" + params.getName() + "%");
        }

        sql += " order by g.createTime desc";

        return sql;
    }

    /**
     * 创建
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(Model model) {
        model.addAttribute("goodsUnit", new LiveChannel()).addAttribute("_method", "post");
        return "mall/live/channel/form";
    }

    /**
     * 创建
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> create(@RequestHeader("X-Requested-With") String requestWith, LiveChannel params) {
        try {
            if (!Webs.isAjaxRequest(requestWith)) {
                throw new IllegalArgumentException("request must be ajax");
            }
            liveChannelApplication.create(params);
            return ImmutableMap.of("status", true, "message", "创建成功");
        } catch (IllegalArgumentException e) {
            return ImmutableMap.of("status", false, "message", e.getMessage());
        } catch (Exception e) {
            log.error("直播分类创建异常", e);
            return ImmutableMap.of("status", false, "message", "创建异常");

        }
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/{id}/modify", method = RequestMethod.GET)
    public String modify(Model model, @PathVariable("id") long id) {
        model.addAttribute("goodsUnit", queryChannelService.findOne(QLiveChannel.liveChannel.id.eq(id), LiveChannel.class)).addAttribute("_method", "PUT");
        return "mall/live/channel/form";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/{id}/modify", method = RequestMethod.PUT)
    @ResponseBody
    public Map<String, Object> modify(HttpServletRequest request, LiveChannel params) {
        try {
            if (!Webs.isAjaxRequest(request)) {
                throw new IllegalArgumentException("request must be ajax");
            }

            liveChannelApplication.modify(params);
            return ImmutableMap.of("status", true, "message", "编辑成功");
        } catch (IllegalArgumentException e) {
            return ImmutableMap.of("status", false, "message", e.getMessage());
        } catch (Exception e) {
            log.error("直播分类编辑异常", e);
            return ImmutableMap.of("status", false, "message", "编辑异常");
        }
    }


    /**
     * 删除
     */
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    @ResponseBody
    public Map<String, Object> remove(HttpServletRequest request, LiveChannel params) {
        try {
            if (!Webs.isAjaxRequest(request)) {
                throw new IllegalArgumentException("request must be ajax");
            }
            liveChannelApplication.remove(queryChannelService.findOne(QLiveChannel.liveChannel.id.eq(params.getId()), LiveChannel.class));
            return ImmutableMap.of("status", true, "message", "删除成功");
        } catch (Exception e) {
            log.error("直播分类删除异常", e);
            return ImmutableMap.of("status", false, "message", "删除异常");
        }
    }
}
